home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / shells / sh03src.zoo / sh-pl03 / sh / exec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-18  |  17.9 KB  |  858 lines

  1. /*-
  2.  * Copyright (c) 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Kenneth Almquist.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. static char sccsid[] = "@(#)exec.c    5.2 (Berkeley) 3/13/91";
  39. #endif /* not lint */
  40.  
  41. /*
  42.  * When commands are first encountered, they are entered in a hash table.
  43.  * This ensures that a full path search will not have to be done for them
  44.  * on each invocation.
  45.  *
  46.  * We should investigate converting to a linear search, even though that
  47.  * would make the command name "hash" a misnomer.
  48.  */
  49.  
  50. #include "shell.h"
  51. #include "main.h"
  52. #include "nodes.h"
  53. #include "parser.h"
  54. #include "redir.h"
  55. #include "eval.h"
  56. #include "exec.h"
  57. #include "builtins.h"
  58. #include "var.h"
  59. #include "options.h"
  60. #include "input.h"
  61. #include "output.h"
  62. #include "syntax.h"
  63. #include "memalloc.h"
  64. #include "error.h"
  65. #include "init.h"
  66. #include "mystring.h"
  67. #include <sys/types.h>
  68. #include <sys/stat.h>
  69. #include <fcntl.h>
  70. #include <errno.h>
  71.  
  72. #ifdef __MINT__
  73. /* Defines order in which to look for executable program suffixes */
  74. static const char *const suffixes[] = {
  75.     "", ".ttp", ".tos", ".prg", 0
  76. };
  77. #endif
  78.  
  79. #define CMDTABLESIZE 31        /* should be prime */
  80. #define ARB 1            /* actual size determined at run time */
  81.  
  82.  
  83.  
  84. struct tblentry {
  85.     struct tblentry *next;    /* next entry in hash chain */
  86.     union param param;    /* definition of builtin function */
  87.     short cmdtype;        /* index identifying command */
  88.     char rehash;        /* if set, cd done since entry created */
  89.     char cmdname[ARB];    /* name of command */
  90. };
  91.  
  92.  
  93. STATIC struct tblentry *cmdtable[CMDTABLESIZE];
  94. STATIC int builtinloc = -1;        /* index in path of %builtin, or -1 */
  95.  
  96.  
  97. #ifdef __STDC__
  98. STATIC void tryexec(char *, char **, char **);
  99. STATIC void execinterp(char **, char **);
  100. STATIC void printentry(struct tblentry *);
  101. STATIC void clearcmdentry(int);
  102. STATIC struct tblentry *cmdlookup(char *, int);
  103. STATIC void delete_cmd_entry(void);
  104. #else
  105. STATIC void tryexec();
  106. STATIC void execinterp();
  107. STATIC void printentry();
  108. STATIC void clearcmdentry();
  109. STATIC struct tblentry *cmdlookup();
  110. STATIC void delete_cmd_entry();
  111. #endif
  112.  
  113.  
  114.  
  115. /*
  116.  * Exec a program.  Never returns.  If you change this routine, you may
  117.  * have to change the find_command routine as well.
  118.  */
  119.  
  120. void
  121. shellexec(argv, envp, path, index)
  122.     char **argv, **envp;
  123.     char *path;
  124.     {
  125.     char *cmdname;
  126.     int e;
  127.  
  128.     if (strchr(argv[0], '/') != NULL) {
  129.         tryexec(argv[0], argv, envp);
  130.         e = errno;
  131.     } else {
  132.         e = ENOENT;
  133.         while ((cmdname = padvance(&path, argv[0])) != NULL) {
  134.             if (--index < 0 && pathopt == NULL) {
  135.                 tryexec(cmdname, argv, envp);
  136.                 if (errno != ENOENT && errno != ENOTDIR)
  137.                     e = errno;
  138.             }
  139.             stunalloc(cmdname);
  140.         }
  141.     }
  142.     error2(argv[0], errmsg(e, E_EXEC));
  143. }
  144.  
  145.  
  146. STATIC void
  147. tryexec(cmd, argv, envp)
  148.     char *cmd;
  149.     char **argv;
  150.     char **envp;
  151.     {
  152.     int e;
  153.     char *p;
  154.  
  155. #ifdef SYSV
  156.     do {
  157.         execve(cmd, argv, envp);
  158.     } while (errno == EINTR);
  159. #else /* !SYSV */
  160. #ifdef __MINT__
  161.     {
  162.         const char *const *suffix;
  163.         long len = strlen(cmd);
  164.  
  165.         char *xcmd = alloca(len+5);
  166.         strcpy(xcmd,cmd);
  167.  
  168.         for (suffix = suffixes; *suffix; suffix++) {
  169.             strcpy(xcmd + len, *suffix);
  170.             execve(xcmd, argv, envp);
  171.             
  172.             if (errno == ENOEXEC) break;
  173.         } 
  174.     }
  175. #else /* !__MINT__, !SYSV */
  176.     execve(cmd, argv, envp);
  177. #endif /* !__MINT__ */
  178. #endif /* !SYSV */
  179.     e = errno;
  180.     if (e == ENOEXEC) {
  181.         initshellproc();
  182.         setinputfile(cmd, 0);
  183.         commandname = arg0 = savestr(argv[0]);
  184. #if defined(__MINT__) || !defined(BSD)
  185.         pgetc(); pungetc();        /* fill up input buffer */
  186.         p = parsenextc;
  187.         if (parsenleft > 2 && p[0] == '#' && p[1] == '!') {
  188.             argv[0] = cmd;
  189.             execinterp(argv, envp);
  190.         }
  191. #endif
  192.         setparam(argv + 1);
  193.         exraise(EXSHELLPROC);
  194.         /*NOTREACHED*/
  195.     }
  196.     errno = e;
  197. }
  198.  
  199.  
  200. #if defined(__MINT__) || !defined(BSD) 
  201. /*
  202.  * Execute an interpreter introduced by "#!", for systems where this
  203.  * feature has not been built into the kernel.  If the interpreter is
  204.  * the shell, return (effectively ignoring the "#!").  If the execution
  205.  * of the interpreter fails, exit.
  206.  *
  207.  * This code peeks inside the input buffer in order to avoid actually
  208.  * reading any input.  It would benefit from a rewrite.
  209.  */
  210.  
  211. #define NEWARGS 5
  212.  
  213. STATIC void
  214. execinterp(argv, envp)
  215.     char **argv, **envp;
  216.     {
  217.     int n;
  218.     char *inp;
  219.     char *outp;
  220.     char c;
  221.     char *p;
  222.     char **ap;
  223.     char *newargs[NEWARGS];
  224.     int i;
  225.     char **ap2;
  226.     char **new;
  227.  
  228.     n = parsenleft - 2;
  229.     inp = parsenextc + 2;
  230.     ap = newargs;
  231.     for (;;) {
  232.         while (--n >= 0 && (*inp == ' ' || *inp == '\t'))
  233.             inp++;
  234.         if (n < 0)
  235.             goto bad;
  236.         if ((c = *inp++) == '\n')
  237.             break;
  238. #ifdef __MINT__
  239.         if (c == '\r')
  240.             break;
  241. #endif
  242.         if (ap == &newargs[NEWARGS])
  243. bad:          error("Bad #! line");
  244.         STARTSTACKSTR(outp);
  245.         do {
  246.             STPUTC(c, outp);
  247.         } 
  248.         while (--n >= 0 && (c = *inp++) != ' ' && c != '\t' 
  249. #ifdef __MINT__
  250.             && c != '\r'
  251. #endif
  252.             && c != '\n');
  253.         STPUTC('\0', outp);
  254.         n++, inp--;
  255.         *ap++ = grabstackstr(outp);
  256.     }
  257.     if (ap == newargs + 1) {    /* if no args, maybe no exec is needed */
  258.         p = newargs[0];
  259.         for (;;) {
  260.             if (equal(p, "sh") || equal(p, "ash")) {
  261.                 return;
  262.             }
  263.             while (*p != '/') {
  264.                 if (*p == '\0')
  265.                     goto break2;
  266.                 p++;
  267.             }
  268.             p++;
  269.         }
  270. break2:;
  271.     }
  272.     i = (char *)ap - (char *)newargs;        /* size in bytes */
  273.     if (i == 0)
  274.         error("Bad #! line");
  275.     for (ap2 = argv ; *ap2++ != NULL ; );
  276.     new = ckmalloc(i + ((char *)ap2 - (char *)argv));
  277.     ap = newargs, ap2 = new;
  278.     while ((i -= sizeof (char **)) >= 0)
  279.         *ap2++ = *ap++;
  280.     ap = argv;
  281.     while (*ap2++ = *ap++);
  282.     shellexec(new, envp, pathval(), 0);
  283. }
  284. #endif
  285.  
  286.  
  287.  
  288. /*
  289.  * Do a path search.  The variable path (passed by reference) should be
  290.  * set to the start of the path before the first call; padvance will update
  291.  * this value as it proceeds.  Successive calls to padvance will return
  292.  * the possible path expansions in sequence.  If an option (indicated by
  293.  * a percent sign) appears in the path entry then the global variable
  294.  * pathopt will be set to point to it; otherwise pathopt will be set to
  295.  * NULL.
  296.  */
  297.  
  298. char *pathopt;
  299.  
  300. char *
  301. padvance(path, name)
  302.     char **path;
  303.     char *name;
  304.     {
  305.     register char *p, *q;
  306.     char *start;
  307.     int len;
  308.  
  309.     if (*path == NULL)
  310.         return NULL;
  311.     start = *path;
  312.     for (p = start ; *p && *p != ':' && *p != '%' ; p++);
  313.     len = p - start + strlen(name) + 2;    /* "2" is for '/' and '\0' */
  314.     while (stackblocksize() < len)
  315.         growstackblock();
  316.     q = stackblock();
  317.     if (p != start) {
  318.         bcopy(start, q, p - start);
  319.         q += p - start;
  320.         *q++ = '/';
  321.     }
  322.     strcpy(q, name);
  323.     pathopt = NULL;
  324.     if (*p == '%') {
  325.         pathopt = ++p;
  326.         while (*p && *p != ':')  p++;
  327.     }
  328.     if (*p == ':')
  329.         *path = p + 1;
  330.     else
  331.         *path = NULL;
  332.     return stalloc(len);
  333. }
  334.  
  335.  
  336.  
  337. /*** Command hashing code ***/
  338.  
  339.  
  340. hashcmd(argc, argv)  char **argv; {
  341.     struct tblentry **pp;
  342.     struct tblentry *cmdp;
  343.     int c;
  344.     int verbose;
  345.     struct cmdentry entry;
  346.     char *name;
  347.  
  348.     if (argc <= 1) {
  349.         for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
  350.             for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
  351.                 printentry(cmdp);
  352.             }
  353.         }
  354.         return 0;
  355.     }
  356.     verbose = 0;
  357.     while ((c = nextopt("rv")) != '\0') {
  358.         if (c == 'r') {
  359.             clearcmdentry(0);
  360.         } else if (c == 'v') {
  361.             verbose++;
  362.         }
  363.     }
  364.     while ((name = *argptr) != NULL) {
  365.         if ((cmdp = cmdlookup(name, 0)) != NULL
  366.          && (cmdp->cmdtype == CMDNORMAL
  367.              || cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))
  368.             delete_cmd_entry();
  369.         find_command(name, &entry, 1);
  370.         if (verbose) {
  371.             if (entry.cmdtype != CMDUNKNOWN) {    /* if no error msg */
  372.                 cmdp = cmdlookup(name, 0);
  373.                 printentry(cmdp);
  374.             }
  375.             flushall();
  376.         }
  377.         argptr++;
  378.     }
  379.     return 0;
  380. }
  381.  
  382.  
  383. STATIC void
  384. printentry(cmdp)
  385.     struct tblentry *cmdp;
  386.     {
  387.     int index;
  388.     char *path;
  389.     char *name;
  390.  
  391.     if (cmdp->cmdtype == CMDNORMAL) {
  392.         index = cmdp->param.index;
  393.         path = pathval();
  394.         do {
  395.             name = padvance(&path, cmdp->cmdname);
  396.             stunalloc(name);
  397.         } while (--index >= 0);
  398.         out1str(name);
  399.     } else if (cmdp->cmdtype == CMDBUILTIN) {
  400.         out1fmt("builtin %s", cmdp->cmdname);
  401.     } else if (cmdp->cmdtype == CMDFUNCTION) {
  402.         out1fmt("function %s", cmdp->cmdname);
  403. #ifdef DEBUG
  404.     } else {
  405.         error("internal error: cmdtype %d", cmdp->cmdtype);
  406. #endif
  407.     }
  408.     if (cmdp->rehash)
  409.         out1c('*');
  410.     out1c('\n');
  411. }
  412.  
  413.  
  414.  
  415. /*
  416.  * Resolve a command name.  If you change this routine, you may have to
  417.  * change the shellexec routine as well.
  418.  */
  419.  
  420. void
  421. find_command(name, entry, printerr)
  422.     char *name;
  423.     struct cmdentry *entry;
  424.     {
  425.     struct tblentry *cmdp;
  426.     int index;
  427.     int prev;
  428.     char *path;
  429.     char *fullname;
  430.     struct stat statb;
  431.     int e;
  432.     int i;
  433.  
  434.     /* If name contains a slash, don't use the hash table */
  435.     if (strchr(name, '/') != NULL) {
  436.         entry->cmdtype = CMDNORMAL;
  437.         entry->u.index = 0;
  438.         return;
  439.     }
  440.  
  441.     /* If name is in the table, and not invalidated by cd, we're done */
  442.     if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->rehash == 0)
  443.         goto success;
  444.  
  445.     /* If %builtin not in path, check for builtin next */
  446.     if (builtinloc < 0 && (i = find_builtin(name)) >= 0) {
  447.         INTOFF;
  448.         cmdp = cmdlookup(name, 1);
  449.         cmdp->cmdtype = CMDBUILTIN;
  450.         cmdp->param.index = i;
  451.         INTON;
  452.         goto success;
  453.     }
  454.  
  455.     /* We have to search path. */
  456.     prev = -1;        /* where to start */
  457.     if (cmdp) {        /* doing a rehash */
  458.         if (cmdp->cmdtype == CMDBUILTIN)
  459.             prev = builtinloc;
  460.         else
  461.             prev = cmdp->param.index;
  462.     }
  463.  
  464.     path = pathval();
  465.     e = ENOENT;
  466.     index = -1;
  467. loop:
  468.     while ((fullname = padvance(&path, name)) != NULL) {
  469.         stunalloc(fullname);
  470.         index++;
  471.         if (pathopt) {
  472.             if (prefix("builtin", pathopt)) {
  473.                 if ((i = find_builtin(name)) < 0)
  474.                     goto loop;
  475.                 INTOFF;
  476.                 cmdp = cmdlookup(name, 1);
  477.                 cmdp->cmdtype = CMDBUILTIN;
  478.                 cmdp->param.index = i;
  479.                 INTON;
  480.                 goto success;
  481.             } else if (prefix("func", pathopt)) {
  482.                 /* handled below */
  483.             } else {
  484.                 goto loop;    /* ignore unimplemented options */
  485.             }
  486.         }
  487.         /* if rehash, don't redo absolute path names */
  488.         if (fullname[0] == '/' && index <= prev) {
  489.             if (index < prev)
  490.                 goto loop;
  491.             TRACE(("searchexec \"%s\": no change\n", name));
  492.             goto success;
  493.         }
  494. #ifdef __MINT__
  495.         {
  496.             const char *const *suffix;
  497.             long len = strlen(fullname);
  498.  
  499.             char *xcmd = alloca(len+5);
  500.             strcpy(xcmd,fullname);
  501.  
  502.             for (suffix = suffixes; *suffix; suffix++) {
  503.                 strcpy(xcmd + len, *suffix);
  504.                 if (stat(xcmd, &statb) >= 0)
  505.                     break;
  506.             } 
  507.             
  508.             if (*suffix == 0)
  509.                 /* All suffixes tried to no avail */
  510.                 goto loop;
  511.         }
  512. #else /* !__MINT__*/
  513.         while (stat(fullname, &statb) < 0) {
  514. # ifdef SYSV
  515.             if (errno == EINTR)
  516.                 continue;
  517. # endif
  518.             if (errno != ENOENT && errno != ENOTDIR)
  519.                 e = errno;
  520.             goto loop;
  521.         }
  522. #endif /* !__MINT__ */
  523.         e = EACCES;    /* if we fail, this will be the error */
  524.         if ((statb.st_mode & S_IFMT) != S_IFREG)
  525.             goto loop;
  526.         if (pathopt) {        /* this is a %func directory */
  527.             stalloc(strlen(fullname) + 1);
  528.             readcmdfile(fullname);
  529.             if ((cmdp = cmdlookup(name, 0)) == NULL || cmdp->cmdtype != CMDFUNCTION)
  530.                 error("%s not defined in %s", name, fullname);
  531.             stunalloc(fullname);
  532.             goto success;
  533.         }
  534.         if (statb.st_uid == geteuid()) {
  535.             if ((statb.st_mode & 0100) == 0)
  536.                 goto loop;
  537.         } else if (statb.st_gid == getegid()) {
  538.             if ((statb.st_mode & 010) == 0)
  539.                 goto loop;
  540.         } else {
  541.             if ((statb.st_mode & 01) == 0)
  542.                 goto loop;
  543.         }
  544.         TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
  545.         INTOFF;
  546.         cmdp = cmdlookup(name, 1);
  547.         cmdp->cmdtype = CMDNORMAL;
  548.         cmdp->param.index = index;
  549.         INTON;
  550.         goto success;
  551.     }
  552.  
  553.     /* We failed.  If there was an entry for this command, delete it */
  554.     if (cmdp)
  555.         delete_cmd_entry();
  556.     if (printerr)
  557.         outfmt(out2, "%s: %s\n", name, errmsg(e, E_EXEC));
  558.     entry->cmdtype = CMDUNKNOWN;
  559.     return;
  560.  
  561. success:
  562.     cmdp->rehash = 0;
  563.     entry->cmdtype = cmdp->cmdtype;
  564.     entry->u = cmdp->param;
  565. }
  566.  
  567.  
  568.  
  569. /*
  570.  * Search the table of builtin commands.
  571.  */
  572.  
  573. int
  574. find_builtin(name)
  575.     char *name;
  576.     {
  577.     const register struct builtincmd *bp;
  578.  
  579.     for (bp = builtincmd ; bp->name ; bp++) {
  580.         if (*bp->name == *name && equal(bp->name, name))
  581.             return bp->code;
  582.     }
  583.     return -1;
  584. }
  585.  
  586.  
  587.  
  588. /*
  589.  * Called when a cd is done.  Marks all commands so the next time they
  590.  * are executed they will be rehashed.
  591.  */
  592.  
  593. void
  594. hashcd() {
  595.     struct tblentry **pp;
  596.     struct tblentry *cmdp;
  597.  
  598.     for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
  599.         for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
  600.             if (cmdp->cmdtype == CMDNORMAL
  601.              || cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)
  602.                 cmdp->rehash = 1;
  603.         }
  604.     }
  605. }
  606.  
  607.  
  608.  
  609. /*
  610.  * Called before PATH is changed.  The argument is the new value of PATH;
  611.  * pathval() still returns the old value at this point.  Called with
  612.  * interrupts off.
  613.  */
  614.  
  615. void
  616. changepath(newval)
  617.     char *newval;
  618.     {
  619.     char *old, *new;
  620.     int index;
  621.     int firstchange;
  622.     int bltin;
  623.  
  624.     old = pathval();
  625.     new = newval;
  626.     firstchange = 9999;    /* assume no change */
  627.     index = 0;
  628.     bltin = -1;
  629.     for (;;) {
  630.         if (*old != *new) {
  631.             firstchange = index;
  632.             if (*old == '\0' && *new == ':'
  633.              || *old == ':' && *new == '\0')
  634.                 firstchange++;
  635.             old = new;    /* ignore subsequent differences */
  636.         }
  637.         if (*new == '\0')
  638.             break;
  639.         if (*new == '%' && bltin < 0 && prefix("builtin", new + 1))
  640.             bltin = index;
  641.         if (*new == ':') {
  642.             index++;
  643.         }
  644.         new++, old++;
  645.     }
  646.     if (builtinloc < 0 && bltin >= 0)
  647.         builtinloc = bltin;        /* zap builtins */
  648.     if (builtinloc >= 0 && bltin < 0)
  649.         firstchange = 0;
  650.     clearcmdentry(firstchange);
  651.     builtinloc = bltin;
  652. }
  653.  
  654.  
  655. /*
  656.  * Clear out command entries.  The argument specifies the first entry in
  657.  * PATH which has changed.
  658.  */
  659.  
  660. STATIC void
  661. clearcmdentry(firstchange) {
  662.     struct tblentry **tblp;
  663.     struct tblentry **pp;
  664.     struct tblentry *cmdp;
  665.  
  666.     INTOFF;
  667.     for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
  668.         pp = tblp;
  669.         while ((cmdp = *pp) != NULL) {
  670.             if (cmdp->cmdtype == CMDNORMAL && cmdp->param.index >= firstchange
  671.              || cmdp->cmdtype == CMDBUILTIN && builtinloc >= firstchange) {
  672.                 *pp = cmdp->next;
  673.                 ckfree(cmdp);
  674.             } else {
  675.                 pp = &cmdp->next;
  676.             }
  677.         }
  678.     }
  679.     INTON;
  680. }
  681.  
  682.  
  683. /*
  684.  * Delete all functions.
  685.  */
  686.  
  687. #ifdef mkinit
  688. MKINIT void deletefuncs();
  689.  
  690. SHELLPROC {
  691.     deletefuncs();
  692. }
  693. #endif
  694.  
  695. void
  696. deletefuncs() {
  697.     struct tblentry **tblp;
  698.     struct tblentry **pp;
  699.     struct tblentry *cmdp;
  700.  
  701.     INTOFF;
  702.     for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
  703.         pp = tblp;
  704.         while ((cmdp = *pp) != NULL) {
  705.             if (cmdp->cmdtype == CMDFUNCTION) {
  706.                 *pp = cmdp->next;
  707.                 freefunc(cmdp->param.func);
  708.                 ckfree(cmdp);
  709.             } else {
  710.                 pp = &cmdp->next;
  711.             }
  712.         }
  713.     }
  714.     INTON;
  715. }
  716.  
  717.  
  718.  
  719. /*
  720.  * Locate a command in the command hash table.  If "add" is nonzero,
  721.  * add the command to the table if it is not already present.  The
  722.  * variable "lastcmdentry" is set to point to the address of the link
  723.  * pointing to the entry, so that delete_cmd_entry can delete the
  724.  * entry.
  725.  */
  726.  
  727. struct tblentry **lastcmdentry;
  728.  
  729.  
  730. STATIC struct tblentry *
  731. cmdlookup(name, add)
  732.     char *name;
  733.     {
  734.     int hashval;
  735.     register char *p;
  736.     struct tblentry *cmdp;
  737.     struct tblentry **pp;
  738.  
  739.     p = name;
  740.     hashval = *p << 4;
  741.     while (*p)
  742.         hashval += *p++;
  743.     hashval &= 0x7FFF;
  744.     pp = &cmdtable[hashval % CMDTABLESIZE];
  745.     for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
  746.         if (equal(cmdp->cmdname, name))
  747.             break;
  748.         pp = &cmdp->next;
  749.     }
  750.     if (add && cmdp == NULL) {
  751.         INTOFF;
  752.         cmdp = *pp = ckmalloc(sizeof (struct tblentry) - ARB
  753.                     + strlen(name) + 1);
  754.         cmdp->next = NULL;
  755.         cmdp->cmdtype = CMDUNKNOWN;
  756.         cmdp->rehash = 0;
  757.         strcpy(cmdp->cmdname, name);
  758.         INTON;
  759.     }
  760.     lastcmdentry = pp;
  761.     return cmdp;
  762. }
  763.  
  764.  
  765. /*
  766.  * Delete the command entry returned on the last lookup.
  767.  */
  768.  
  769. STATIC void
  770. delete_cmd_entry() {
  771.     struct tblentry *cmdp;
  772.  
  773.     INTOFF;
  774.     cmdp = *lastcmdentry;
  775.     *lastcmdentry = cmdp->next;
  776.     ckfree(cmdp);
  777.     INTON;
  778. }
  779.  
  780.  
  781.  
  782. #ifdef notdef
  783. void
  784. getcmdentry(name, entry)
  785.     char *name;
  786.     struct cmdentry *entry; 
  787.     {
  788.     struct tblentry *cmdp = cmdlookup(name, 0);
  789.  
  790.     if (cmdp) {
  791.         entry->u = cmdp->param;
  792.         entry->cmdtype = cmdp->cmdtype;
  793.     } else {
  794.         entry->cmdtype = CMDUNKNOWN;
  795.         entry->u.index = 0;
  796.     }
  797. }
  798. #endif
  799.  
  800.  
  801. /*
  802.  * Add a new command entry, replacing any existing command entry for
  803.  * the same name.
  804.  */
  805.  
  806. void
  807. addcmdentry(name, entry)
  808.     char *name;
  809.     struct cmdentry *entry;
  810.     {
  811.     struct tblentry *cmdp;
  812.  
  813.     INTOFF;
  814.     cmdp = cmdlookup(name, 1);
  815.     if (cmdp->cmdtype == CMDFUNCTION) {
  816.         freefunc(cmdp->param.func);
  817.     }
  818.     cmdp->cmdtype = entry->cmdtype;
  819.     cmdp->param = entry->u;
  820.     INTON;
  821. }
  822.  
  823.  
  824. /*
  825.  * Define a shell function.
  826.  */
  827.  
  828. void
  829. defun(name, func)
  830.     char *name;
  831.     union node *func;
  832.     {
  833.     struct cmdentry entry;
  834.  
  835.     INTOFF;
  836.     entry.cmdtype = CMDFUNCTION;
  837.     entry.u.func = copyfunc(func);
  838.     addcmdentry(name, &entry);
  839.     INTON;
  840. }
  841.  
  842.  
  843. /*
  844.  * Delete a function if it exists.
  845.  */
  846.  
  847. void
  848. unsetfunc(name)
  849.     char *name;
  850.     {
  851.     struct tblentry *cmdp;
  852.  
  853.     if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) {
  854.         freefunc(cmdp->param.func);
  855.         delete_cmd_entry();
  856.     }
  857. }
  858.